package com.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.Comment;
import com.serve.CommentService;
import com.serveImple.CommentServiceImpl;

/**
 * Servlet implementation class CommentServlet
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
				
		Comment comment=new Comment();
		String st=request.getParameter("content");
		if(st.equals("")){
			request.setAttribute("tishi", "评论内容不能为空");
			request.getRequestDispatcher("easybuy/goodsShow.jsp").forward(request, response);
		}
		
		String str=request.getParameter("name");
		System.out.println(str);
		
		int g_id=Integer.parseInt(str);
		
		request.setAttribute("gid", g_id);
		comment.setG_id(g_id);
		comment.setC_content(st);
	CommentService commentservice=new CommentServiceImpl();
		int i= commentservice.add(comment);
		if(i!=1){
			request.setAttribute("jie", 1);
			request.setAttribute("jieguo", i);
			System.out.println("评论失败");
			request.getRequestDispatcher("easybuy/goodsShow.jsp").forward(request, response);
		}else{
			request.setAttribute("jie", 1);
			request.setAttribute("jieguo", i);
			System.out.println("评论成功");
			request.getRequestDispatcher("CommentShowServlet").forward(request, response);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request,response);
	}

}
